package com.ebt.m.customer.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.event.TapeMessageViewEvent;
import com.ebt.m.data.bean.BeanCustomerTape;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * created by rick.wang
 */
public class TapeMessageView extends LinearLayout implements View.OnClickListener,
        MediaPlayer.OnCompletionListener{

    enum State{
        Idle, Pause, Playing
    }

    private BeanCustomerTape mTape;
    private State mState = State.Idle;
    private float mProgress = 0f;//[0,1]
    private LinearLayout tape_ll;
    private TextView  tape_tv;
    private ImageView tape_iv;
    private AnimationDrawable anim;

    public TapeMessageView(Context context) {
        super(context);
        init(context);
    }
    public TapeMessageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TapeMessageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        View contentView = inflate(context, R.layout.tape_message_view, this);
        tape_ll = (LinearLayout) contentView.findViewById(R.id.tape_ll);
        tape_ll.setOnClickListener(this);
        tape_iv = (ImageView)contentView.findViewById(R.id.tape_iv);
        tape_iv.setOnClickListener(this);
        tape_tv = (TextView)contentView.findViewById(R.id.tape_tv);
        tape_tv.setOnClickListener(this);
        anim = (AnimationDrawable) getResources().getDrawable(R.drawable.frame_tape_play);
    }

    private AnimationDrawable getAnim(){
        if(anim == null){
            anim = (AnimationDrawable) getResources().getDrawable(R.drawable.frame_tape_play);
        }
        return anim;
    }

    public void update(BeanCustomerTape tape){
        if(tape == null) {
            Log.d("TapeView", "exception in tapeview, tapeWrapper is null!");
        }
        mTape = tape;
        tape_tv.setText(String.valueOf(mTape.getTapeDuration()) + "\"");
    }

    private int getWidthByMaxWidthPx(int duration, int maxWidPx){
        float ratio = ((float)duration )/ 60;
        if(ratio > 1){
            return maxWidPx;
        }
        return (int)(0.3 * maxWidPx + 0.7 * maxWidPx * ratio);
    }

    public void setProgress(float progress){
        if(0 <= progress && progress <= 1){
            mProgress = progress;
        }
    }

    public float getProgress(){
        return mProgress;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tape_tv:
            case R.id.tape_iv:
            case R.id.tape_ll:
                clickMessage();
            break;
        }
    }

    private void clickMessage(){
        if(mTape == null) return;
        switch (mState){
            case Idle:
                EventBus.getDefault().post(new TapeMessageViewEvent(mTape.getUuid()));
                if(mTapeOperate != null){
                    mTapeOperate.onTapeToPlay(this, mTape, mProgress);
                }
                tape_iv.setImageDrawable(getAnim());
                anim.start();
                mState = State.Playing;
                break;
            case Pause:
                EventBus.getDefault().post(new TapeMessageViewEvent(mTape.getUuid()));
                if(mTapeOperate != null){
                    mTapeOperate.onTapeToPlay(this, mTape, mProgress);
                }
                tape_iv.setImageDrawable(getAnim());
                anim.start();
                mState = State.Playing;
                break;
            case Playing:
                if(mTapeOperate != null){
                    mTapeOperate.onTapeToPause(this, mTape, mProgress);
                }
                tape_iv.setImageResource(R.drawable.tape_play_3);
                mState = State.Pause;
                break;
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        stopPlayingForUI();
    }

    private void stopPlayingForUI(){
        if(mState != State.Pause) {
            tape_iv.setImageResource(R.drawable.tape_play_3);
            mState = State.Pause;
        }
    }

    private void stopPlayingForPlayer(){
        //TODO
    }

    public interface TapeOperate{
        void onTapeToPlay(TapeMessageView view, BeanCustomerTape tape, float progress);
        void onTapeToPause(TapeMessageView view, BeanCustomerTape tape, float progress);
    }

    private TapeOperate mTapeOperate;
    public void setTapeOperate(TapeOperate operate){
        mTapeOperate = operate;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(TapeMessageViewEvent event) {
        if(event != null && mTape != null && !mTape.getUuid().equals(event.uuid)){
            stopPlayingForUI();
            stopPlayingForPlayer();
        }
    }
}
